package cn.com.cetccst.uicomponents.binding.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.databinding.BindingAdapter;

import cn.com.cetccst.uicomponents.R;

public class CircleProgressbar extends View {

    private static final String TAG = "CircleProgressBar";
    private final int color;
    private final int color_bg;
    private final boolean isAppStyle;
    private int mMaxProgress = 100;
    // start at 0 is not good looking.
    private final int startProgress = 3;
    private int mProgress = startProgress;

    // 画圆所在的距形区域
    private final RectF mRectF;
    private final Paint mPaint;
    private String mTxtHint1;
    private String mTxtHint2;

    public CircleProgressbar(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.CircleProgressbar);
        color = arr.getColor(R.styleable.CircleProgressbar_color, ContextCompat.getColor(context, R.color.uicomponents_theme_color));
        color_bg = arr.getColor(R.styleable.CircleProgressbar_color_bg, ContextCompat.getColor(context, R.color.uicomponents_progress_bg));
        isAppStyle = arr.getBoolean(R.styleable.CircleProgressbar_apple_style, false);
        arr.recycle();

        mRectF = new RectF();
        mPaint = new Paint();
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        int width = this.getWidth();
        int height = this.getHeight();

        if (width != height) {
            int min = Math.min(width, height);
            width = min;
            height = min;
        }

        if (isAppStyle) {
            drawAppleStyle(canvas, width, height);
        } else {
            drawDefaultStyle(canvas, width, height);
        }

    }

    private void drawAppleStyle(Canvas canvas, int width, int height) {
        mPaint.setAntiAlias(true);
        mPaint.setColor(color);
//        mPaint.setAlpha(alpha);
        mRectF.left = 0; // 左上角x
        mRectF.top = 0; // 左上角y
        mRectF.right = width; // 左下角x
        mRectF.bottom = height; // 右下角y

        float progress = ((float) mProgress / mMaxProgress) * 360;
        canvas.drawArc(mRectF, -90 + progress, 360 - progress, true, mPaint);
    }

    private void drawDefaultStyle(Canvas canvas, int width, int height) {
        // 设置画笔相关属性
        mPaint.setAntiAlias(true);
//        mPaint.setColor(Color.rgb(0xe9, 0xe9, 0xe9));
        mPaint.setColor(color_bg);
//        mPaint.setAlpha(alpha);
        canvas.drawColor(Color.TRANSPARENT);
        int mCircleLineStrokeWidth = 12;
        mPaint.setStrokeWidth(mCircleLineStrokeWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        // 位置
        mRectF.left = (float) mCircleLineStrokeWidth / 2; // 左上角x
        mRectF.top = (float) mCircleLineStrokeWidth / 2; // 左上角y
        mRectF.right = width - (float) mCircleLineStrokeWidth / 2; // 左下角x
        mRectF.bottom = height - (float) mCircleLineStrokeWidth / 2; // 右下角y

        // 绘制圆圈，进度条背景
        canvas.drawArc(mRectF, -90, 360, false, mPaint);
        mPaint.setColor(color);
//        mPaint.setAlpha(alpha);
        canvas.drawArc(mRectF, -90, ((float) mProgress / mMaxProgress) * 360, false, mPaint);

        // 绘制进度文案显示
        int mTxtStrokeWidth = 2;
        mPaint.setStrokeWidth(mTxtStrokeWidth);
        String text = "| |";//mProgress + "%";
        int textHeight = height / 2;
        mPaint.setTextSize(textHeight);
        int textWidth = (int) mPaint.measureText(text, 0, text.length());
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawText(text, (float) width / 2 - (float) textWidth / 2, (float) height / 2 + (float) textHeight / 2 - 10, mPaint);

        if (!TextUtils.isEmpty(mTxtHint1)) {
            mPaint.setStrokeWidth(mTxtStrokeWidth);
            text = mTxtHint1;
            textHeight = height / 8;
            mPaint.setTextSize(textHeight);
            mPaint.setColor(Color.rgb(0x99, 0x99, 0x99));
//            mPaint.setAlpha(alpha);
            textWidth = (int) mPaint.measureText(text, 0, text.length());
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawText(text, (float) width / 2 - (float) textWidth / 2, (float) height / 4 + (float) textHeight / 2, mPaint);
        }

        if (!TextUtils.isEmpty(mTxtHint2)) {
            mPaint.setStrokeWidth(mTxtStrokeWidth);
            text = mTxtHint2;
            textHeight = height / 8;
            mPaint.setTextSize(textHeight);
            textWidth = (int) mPaint.measureText(text, 0, text.length());
            mPaint.setStyle(Paint.Style.FILL);
            canvas.drawText(text, (float) width / 2 - (float) textWidth / 2, (float) (3 * height) / 4 + (float) textHeight / 2, mPaint);
        }
    }

    public int getMaxProgress() {
        return mMaxProgress;
    }

    public void setMaxProgress(int maxProgress) {
        this.mMaxProgress = maxProgress;
    }

    public void setProgress(int progress) {
        this.mProgress = progress + startProgress;
        this.invalidate();
    }

    public void setProgressNotInUiThread(int progress) {
        this.mProgress = progress + startProgress;
        this.postInvalidate();
    }

    public String getmTxtHint1() {
        return mTxtHint1;
    }

    public void setmTxtHint1(String mTxtHint1) {
        this.mTxtHint1 = mTxtHint1;
    }

    public String getmTxtHint2() {
        return mTxtHint2;
    }

    public void setmTxtHint2(String mTxtHint2) {
        this.mTxtHint2 = mTxtHint2;
    }

    @BindingAdapter(value = "progress")
    public static void progress(final CircleProgressbar circleProgressbar, int progress) {
        if (circleProgressbar != null) {
            circleProgressbar.setProgress(progress);
        }
    }
}